package com.xj.edu.hotel.ExceptionHandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ControllerAdvice
public class RestExceptionHandler {

    //日志
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * 默认异常处理统一方式
     * 默认Exception异常对象
     */
    public  static final String ERROR_VIEW="/common/500";
    @ExceptionHandler(value = Exception.class)
    @ResponseStatus
    public Object errorHandler(Exception e, HttpServletResponse response, HttpServletRequest request) throws Exception{
       // e.printStackTrace();//打印异常信息
        System.out.println(e.getMessage());
        System.out.println(e.toString());
        System.out.println(e.getLocalizedMessage());
        ModelAndView mv = new ModelAndView();
        mv.addObject("exception",e.toString());
        mv.addObject("url",request.getRequestURI());
        mv.setViewName(ERROR_VIEW);

        return mv;
    }
}
